<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <base data-ice="baseUrl" href="../../">
  <title data-ice="title">source/UMD.js | amd-bundle</title>
  <link type="text/css" rel="stylesheet" href="css/style.css">
  <link type="text/css" rel="stylesheet" href="css/prettify-tomorrow.css">
  <script src="script/prettify/prettify.js"></script>
  <script src="script/manual.js"></script>
<meta name="description" content="A dual-use tool for bundling up AMD modules, which is much easier to use than r.js with Almond or AMDclean"><meta property="twitter:card" content="summary"><meta property="twitter:title" content="amd-bundle"><meta property="twitter:description" content="A dual-use tool for bundling up AMD modules, which is much easier to use than r.js with Almond or AMDclean"></head>
<body class="layout-container" data-ice="rootContainer">

<header>
  <a href="./">Home</a>
  
  <a href="identifiers.html">Reference</a>
  <a href="source.html">Source</a>
  <a href="test.html" data-ice="testLink">Test</a>
  <div class="search-box">
  <span>
    <img src="./image/search.png">
    <span class="search-input-edge"></span><input class="search-input"><span class="search-input-edge"></span>
  </span>
    <ul class="search-result"></ul>
  </div>
<a style="position:relative; top:3px;" href="https://github.com/TechQuery/AMD_bundle.js"><img width="20px" src="./image/github.png"></a></header>

<nav class="navigation" data-ice="nav"><div>
  <ul>
    
  <li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/source/Module.js~Module.html">Module</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/source/Package.js~Package.html">Package</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-generate">generate</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-getNPMFile">getNPMFile</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-getNPMIndex">getNPMIndex</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-getNPMPackage">getNPMPackage</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-merge">merge</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-outPackage">outPackage</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-typedef">T</span><span data-ice="name"><span><a href="typedef/index.html#static-typedef-DependencyMap">DependencyMap</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-typedef">T</span><span data-ice="name"><span><a href="typedef/index.html#static-typedef-NameMap">NameMap</a></span></span></li>
</ul>
</div>
</nav>

<div class="content" data-ice="content"><h1 data-ice="title">source/UMD.js</h1>
<pre class="source-code line-number raw-source-code"><code class="prettyprint linenums" data-ice="content">import { toES_5 } from &apos;@tech_query/node-toolkit&apos;;

import { merge, outPackage } from &apos;./utility&apos;;


function concatModule(pack, name, modName, varName) {  /* eslint-disable */

    return toES_5(`
    var _module_ = {
        ${
    Array.from(pack,  item =&gt; `
        &apos;${item.name}&apos;:  {
            base:        &apos;${item.base}&apos;,
            dependency:  ${JSON.stringify( Object.keys( item.dependency.compile ) )},
            factory:     ${item.source}
        }`.slice(1)
    ).concat(
        modName.map((name, index)  =&gt;  `&apos;${name}&apos;:  {exports: ${varName[index]}}`)
    ).join(&apos;,\n&apos;)}
    };
`,
        name
    );
}

/**
 * @param {Package}       pack
 * @param {string}        name    - Module name of bundled package
 * @param {string}        entry   - Name of entry module (before bundling)
 * @param {DependencyMap} outside
 *
 * @return {string} Bundled source with UMD wrapper
 */
export function generate(pack, name, entry, outside) {

    const modName = Object.keys( outside ), varName = Object.values( outside );

    const dependency = modName[0]  ?  `${JSON.stringify( modName )}, `  :  &apos;&apos;;

    return `
//
//  Generated by https://www.npmjs.com/package/amd-bundle
//
(function (factory) {

    if ((typeof define === &apos;function&apos;)  &amp;&amp;  define.amd)
        define(&apos;${name}&apos;, ${dependency}factory);
    else if (typeof module === &apos;object&apos;)
        return  module.exports = factory.call(${
            [&apos;global&apos;].concat( modName.map(name =&gt; `require(&apos;${name}&apos;)`) )
        });
    else
        return  this[&apos;${name}&apos;] = factory.call(${
            [&apos;self&apos;].concat( modName.map(name =&gt; `this[&apos;${name}&apos;]`) )
        });

})(function (${varName}) {

${merge}

${outPackage}

    var require = (typeof module === &apos;object&apos;) ?
        module.require  :  (
            this.require  ||  function (name) {

                if (self[name] != null)  return self[name];

                throw ReferenceError(&apos;Can\\&apos;t find &quot;&apos; + name + &apos;&quot; module&apos;);
            }
        );

    var _include_ = include.bind(null, &apos;./&apos;);

    function include(base, path) {

        path = outPackage( path )  ?  path  :  (&apos;./&apos; + merge(base, path));

        var module = _module_[path], exports;

        if (! module)  return require(path);

        if (! module.exports) {

            module.exports = { };

            var dependency = module.dependency;

            for (var i = 0;  dependency[i];  i++)
                module.dependency[i] = _include_( dependency[i] );

            exports = module.factory.apply(
                null,  module.dependency.concat(
                    include.bind(null, module.base),  module.exports,  module
                )
            );

            if (exports != null)  module.exports = exports;

            delete module.dependency;  delete module.factory;
        }

        return module.exports;
    }

${concatModule(pack, name, modName, varName)}

    return _include_(&apos;${entry}&apos;);
});`.trim();
}
</code></pre>

</div>

<footer class="footer">
  Generated by <a href="https://esdoc.org">ESDoc<span data-ice="esdocVersion">(1.1.0)</span><img src="./image/esdoc-logo-mini-black.png"></a>
</footer>

<script src="script/search_index.js"></script>
<script src="script/search.js"></script>
<script src="script/pretty-print.js"></script>
<script src="script/inherited-summary.js"></script>
<script src="script/test-summary.js"></script>
<script src="script/inner-link.js"></script>
<script src="script/patch-for-local.js"></script>
</body>
</html>
